package flst.ipiweb.struts.action.codepromo;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.service.ServiceCodePromo;
import flst.ipiweb.service.ServiceDate;
import flst.ipiweb.service.ServiceEnseigne;
import flst.ipiweb.transverse.CodePromotionnel;
import flst.ipiweb.transverse.Enseigne;
import flst.ipiweb.transverse.TypePromo;

public class UpdateCodePromoAction extends Action{
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
		
			//On récupère la session
			HttpSession session = request.getSession();
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
		
			//Récupération du code promo
			ServiceCodePromo sCodePromo = new ServiceCodePromo();
			int idCodePromo = Integer.parseInt(request.getParameter("id"));
			CodePromotionnel codePromo = sCodePromo.getCodePromoById(idCodePromo);
			request.setAttribute("codePromo", codePromo);
			
			//Gestion des dates
			ServiceDate sDate = new ServiceDate();
			String dateDebut = sDate.convertDateToString(codePromo.getDateDebut());
			String dateFin = sDate.convertDateToString(codePromo.getDateFin());
			request.setAttribute("dateDebut", dateDebut);
			request.setAttribute("dateFin", dateFin);
			
			//Récupération des enseignes
			ServiceEnseigne sEnseigne = new ServiceEnseigne();
			List<Enseigne> listEnseigne = sEnseigne.listerEnseigne();
			request.setAttribute("listEnseigne", listEnseigne);
			
			//Récupération liste des types de promo
			TypePromo[] typePromo = TypePromo.values();
			request.setAttribute("typePromo", typePromo);
		
			//Titre de la page
			request.setAttribute("pageTitle", "codepromo.new.title");
			//Activation de l'onglet codepromo
			HashMap<String, String> onglet = new HashMap<String, String>();
			onglet.put("codepromo", "active");
			request.setAttribute("onglet", onglet);
			//Action du formulaire
			request.setAttribute("action","/update-codepromo-post");
			
			return mapping.findForward("update_code_promo");
		}

}
